﻿using Microsoft.EntityFrameworkCore;
using System.Runtime.CompilerServices;

namespace TravelServer.Helper
{
    /// <summary>
    /// 分页数据模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PageNationList<T> : List<T>
    {
        public int TotalPages { get; private set; }
        public int TotalCount { get; private set; }
        public bool HasPrevious => PageNumber > 1;
        public bool HasNext => PageNumber < TotalPages;



        private int pageNumber = 1;
        public int PageNumber
        {
            get
            {
                return pageNumber;
            }
            set
            {
                if (pageNumber > 0 && pageNumber != value) { pageNumber = value; }
            }
        }

        private int pageSize = 5;
        public int PageSize { get { return pageSize; } set { if (pageSize > 0 && pageSize != value) { pageSize = value; } } }

        public PageNationList(int totalcount,int pagenumber, int pagesize,List<T> items)
        {
            this.pageNumber = pagenumber;
            this.pageSize = pagesize;
            AddRange(items);
            this.TotalCount = totalcount;
            //向上取整数
            
            this.TotalPages=(int)Math.Ceiling(totalcount*1.0f/pagesize);
        }
        public static async Task<PageNationList<T>> CreateFromFactory( int pagenumber, int pagesize, IQueryable<T> result)
        {
            int totalcount=await result.CountAsync();
            var list = await result.Skip((pagenumber - 1) * pagesize).Take(pagesize).ToListAsync();
            var page = new PageNationList<T>(totalcount,pagenumber, pagesize,list);
           
            return page;

        }

    }
}
